#include "patientview.h"
#include "ui_patientview.h"
#include "idatabase.h"

PatientView::PatientView(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::PatientView)
{
    ui->setupUi(this);

    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setAlternatingRowColors(true);


    IDatabase &iDataBase = IDatabase::getInstance();
    if(iDataBase.initPatientModel()){
        ui->tableView->setModel(iDataBase.patientTabModel);
        ui->tableView->setSelectionModel(iDataBase.thePatientSelModel);
    }
}

PatientView::~PatientView()
{
    delete ui;
}

void PatientView::on_btnAdd_clicked()
{
    int curRow = IDatabase::getInstance().addNewPatient();

    emit goPatientEditView(curRow);
}

void PatientView::on_btnDelete_clicked()
{
    IDatabase::getInstance().deletePatient();
}

//查询
void PatientView::on_btnSearch_clicked()
{
    QString filter =QString("name like '%%1%'").arg(ui->searchKey->text()) ;
    IDatabase::getInstance().searchPatient(filter);
}


void PatientView::on_btnUpdate_clicked()
{
    QModelIndex curIndex = IDatabase::getInstance().thePatientSelModel->currentIndex();

    emit goPatientEditView(curIndex.row());
}

